<template>
  <el-dialog
    v-el-drag-dialog
    :title="!dataForm.id ? '回复' : '回复'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <!-- <comment :comments="commentData"></comment> -->
    <div class="container">
    <div class="comment" v-for="item in commentData">
      <div class="info">
        <img class="avatar" :src="'https://image.yilianbaihui.cn/consumer/' + item.headImg" width="36" height="36"/>
        <div class="right">
          <div class="name">{{item.realName}}</div>
          <div class="date">{{item.date}}</div>
        </div>
      </div>
      <div>
        <ul v-for="imgs in item.img" style="list-style:none; float:left">
          <li>
            <el-popover
              placement="right"
              title=""
              trigger="hover">
              <img :src="'http://image.yilianbaihui.cn/consumer/'+imgs"/>
              <img slot="reference" :src="'http://image.yilianbaihui.cn/consumer/'+imgs" style="max-height: 30px;max-width: 80px">
            </el-popover></li>
          </ul>
      </div>
      <div v-if="item.video != null && item.video.length > 0">
        <video width="120" height="100" controls>
          <source :src="'https://image.yilianbaihui.cn/consumer/' + item.video[0]">
        </video>
      </div>
      <div class="content">{{item.content}}</div>
      <div class="control">
        <span class="like" :class="{active: item.isLike}" @click="likeClick(item)">
          <i class="iconfont icon-like"></i>
          <span class="like-num">{{item.praseCount > 0 ? item.praseCount + '人赞' : '赞'}}</span>
        </span>
        <span class="comment-reply" @click="showCommentInput(item)">
          <i class="iconfont icon-comment"></i>
          <span>回复</span>
        </span>
      </div>
      <div class="reply">
        <div class="item" v-for="reply in item.reply">
          <div class="reply-content">
            <span class="from-name">{{reply.replyRealName}}</span><span>: </span>
            <span class="to-name">@{{reply.realName}}</span>
            <span>{{reply.content}}</span>
          </div>
          <div class="reply-bottom">
            <span>{{reply.date}}</span>
            <span class="reply-text" @click="showCommentInput(item, reply)">
              <i class="iconfont icon-comment"></i>
              <span>回复</span>
            </span>
          </div>
        </div>
        <!-- <div class="write-reply" v-if="item.reply.length > 0" @click="showCommentInput(item)">
          <i class="el-icon-edit"></i>
          <span class="add-comment">添加新评论</span>
        </div> -->
        <transition name="fade">
          <div class="input-wrapper" v-if="showItemId === item.id">
            <el-input class="gray-bg-input"
                      v-model="inputComment"
                      type="textarea"
                      :rows="3"
                      autofocus
                      placeholder="写下你的评论">
            </el-input>
            <div class="btn-control">
              <span class="cancel" @click="cancel">取消</span>
              <el-button class="btn" type="success" round @click="commitComment">确定</el-button>
            </div>
          </div>
        </transition>
      </div>
    </div>
    </div>
    <span slot="footer" class="dialog-footer" v-if="this.dataForm.adoptShow === 'true'">
      <el-button type="danger" @click="adopt(dataForm.id, 2)">审核不通过</el-button>
      <el-button type="warning" @click="adopt(dataForm.id, 1)">审核通过</el-button>
    </span>
  </el-dialog>
</template>

<script>
  // import * as CommentData from '@/utils/comment'
  // import comment from '../comment/cc'
  import Vue from 'vue'
  export default {
    // components: {
    //   comment
    // },
    // props: {
    //   commentData: {
    //     type: Array,
    //     required: true
    //   }
    // },
    data () {
      return {
        isDisabled: false,
        visible: false,
        dialogVisible: false,
        commentData: [],
        inputComment: '',
        showItemId: '',
        // 子评论的id
        replyId: '',
        // 父id
        parentId: '',
        
        dataForm: {
          id: 0,
          commentList: [],
          auditReplyCount: '',
          type: '',
          adoptShow: 'true',
          commentType: ''
        }
      }
    },
    // created () {
    //   this.commentData = CommentData.comment.data
    //   console.log(this.commentData)
    // },
    methods: {
      init (commentId, type, auditReplyCount) {
        this.dataForm.commentType = type
        if (type === 2) {
          this.dataForm.adoptShow = 'false'
        } else {
          this.dataForm.adoptShow = 'true'
        }
        this.inputComment = ''
        this.showItemId = ''
        this.replyId = ''
        this.parentId = ''
        this.commentData = []
        this.dataForm.id = commentId || 0
        this.dataForm.type = ''
        this.dataForm.type = type
        this.dataForm.auditReplyCount = ''
        this.dataForm.auditReplyCount = auditReplyCount
        this.visible = true
        this.isDisabled = false
        // this.$nextTick(() => {
        //   this.$refs['dataForm'].resetFields()
        if (this.dataForm.id) {
          this.$http({
            url: this.$http.adornUrl(`/good/comment/getCommentReplyListById/${commentId}`),
            method: 'get',
            params: this.$http.adornParams()
          }).then(({data}) => {
            if (data && data.code === 0) {
              this.commentData.push(data.data)
            }
          })
        }
        // })
      },
      /**
       * 点赞
       */
      likeClick (item) {
        if (item.isLike === null) {
          Vue.$set(item, 'isLike', true)
          item.praseCount++
        } else {
          if (item.isLike) {
            item.praseCount--
          } else {
            item.praseCount++
          }
          item.isLike = !item.isLike
        }
      },

      /**
       * 点击取消按钮
       */
      cancel () {
        this.showItemId = ''
        if (this.dataForm.commentType === 2) {
          this.dataForm.adoptShow = 'false'
        } else {
          this.dataForm.adoptShow = 'true'
        }
      },

      /**
       * 提交评论
       */
      commitComment () {
        console.log(this.inputComment)
        this.$http({
          url: this.$http.adornUrl(`/good/comment/addCommentReply`),
          method: 'post',
          data: this.$http.adornData({
            'commentId': this.showItemId,
            'replyId': this.replyId,
            'content': this.inputComment,
            'parent': this.parentId
          })
        }).then(({data}) => {
          if (data && data.code === 0) {
            this.$message('回复成功')
            this.init(this.dataForm.id, this.dataForm.type, this.dataForm.auditReplyCount)
            this.cancel()
          } else {
            this.$message.error(data.msg)
          }
        })
      },

      /**
       * 点击评论按钮显示输入框
       * item: 当前大评论
       * reply: 当前回复的评论
       */
      showCommentInput (item, reply) {
        this.dataForm.adoptShow = 'false'
        if (reply) {
          this.inputComment = '@' + reply.replyRealName + ' '
        } else {
          this.inputComment = ''
        }
        if (reply != null) {
          this.replyId = reply.id
          this.parentId = reply.parentId
        }
        this.showItemId = item.id
      },
      adopt (id, type) {
        let ids = []
        ids.push(id)
        console.log(this.dataForm.auditReplyCount)
        if (this.dataForm.auditReplyCount === 0) {
          this.$http({
            url: this.$http.adornUrl(`/good/comment/modifyAuditStatusById/${type}`),
            method: 'post',
            data: this.$http.adornData(ids, false)
          }).then(({data}) => {
            if (data && data.code === 0) {
              this.$message({
                message: '操作成功',
                type: 'success',
                duration: 1500,
                onClose: () => {
                  this.visible = false
                  this.$emit('refreshDataList')
                }
              })
            } else {
              this.$message.error(data.msg)
            }
          })
        }
        if (this.dataForm.auditReplyCount > 0) {
          this.$http({
            url: this.$http.adornUrl(`/good/comment/modifyAuditReplyById/${type}`),
            method: 'post',
            data: this.$http.adornData(ids, false)
          }).then(({data}) => {
            if (data && data.code === 0) {
              this.$message({
                message: '操作成功',
                type: 'success',
                duration: 1500,
                onClose: () => {
                  this.visible = false
                  this.$emit('refreshDataList')
                }
              })
            } else {
              this.$message.error(data.msg)
            }
          })
        }
      }
    }
  }
</script>
<style scoped lang="scss">
//   @import "../../public/scss/index";
  $color-main: #409EFF;
  $color-success: #67C23A;
  $color-warning: #E6A23C;
  $color-danger: #F56C6C;
  $color-info: #909399;
  $text-main: #303133;
  $text-normal: #606266;
  $text-minor: #909399;  //次要文字
  $text-placeholder: #C0C4CC;
  $text-333: #333;
  $border-first: #DCDFE6;
  $border-second: #E4E7ED;
  $border-third: #EBEEF5;
  $border-fourth: #F2F6FC;
  $content-bg-color: #fff;

  .container {
    padding: 0 10px;
    box-sizing: border-box;
    .comment {
      display: flex;
      flex-direction: column;
      padding: 10px;
      border-bottom: 1px solid $border-fourth;
      .info {
        display: flex;
        align-items: center;
        .avatar {
          border-radius: 50%;
        }
        .right {
          display: flex;
          flex-direction: column;
          margin-left: 10px;
          .name {
            font-size: 16px;
            color: $text-main;
            margin-bottom: 5px;
            font-weight: 500;
          }
          .date {
            font-size: 12px;
            color: $text-minor;
          }
        }
      }
      .content {
        font-size: 16px;
        color: $text-main;
        line-height: 20px;
        padding: 10px 0;
      }
      .control {
        display: flex;
        align-items: center;
        font-size: 14px;
        color: $text-minor;
        .like {
          display: flex;
          align-items: center;
          margin-right: 20px;
          cursor: pointer;
          &.active, &:hover {
            color: $color-main;
          }
          .iconfont {
            font-size: 14px;
            margin-right: 5px;
          }
        }
        .comment-reply {
          display: flex;
          align-items: center;
          cursor: pointer;
          &:hover {
            color: $text-333;
          }
          .iconfont {
            font-size: 16px;
            margin-right: 5px;
          }
        }

      }
      .reply {
        margin: 10px 0;
        border-left: 2px solid $border-first;
        .item {
          margin: 0 10px;
          padding: 10px 0;
          border-bottom: 1px dashed $border-third;
          .reply-content {
            display: flex;
            align-items: center;
            font-size: 14px;
            color: $text-main;
            .from-name {
              color: $color-main;
            }
            .to-name {
              color: $color-main;
              margin-left: 5px;
              margin-right: 5px;
            }
          }
          .reply-bottom {
            display: flex;
            align-items: center;
            margin-top: 6px;
            font-size: 12px;
            color: $text-minor;
            .reply-text {
              display: flex;
              align-items: center;
              margin-left: 10px;
              cursor: pointer;
              &:hover {
                color: $text-333;
              }
              .icon-comment {
                margin-right: 5px;
              }
            }
          }
        }
        .write-reply {
          display: flex;
          align-items: center;
          font-size: 14px;
          color: $text-minor;
          padding: 10px;
          cursor: pointer;
          &:hover {
            color: $text-main;
          }
          .el-icon-edit {
            margin-right: 5px;
          }
        }
        .fade-enter-active, fade-leave-active {
          transition: opacity 0.5s;
        }
        .fade-enter, .fade-leave-to {
          opacity: 0;
        }
        .input-wrapper {
          padding: 10px;
          .gray-bg-input, .el-input__inner {
            /*background-color: #67C23A;*/
          }
          .btn-control {
            display: flex;
            justify-content: flex-end;
            align-items: center;
            padding-top: 10px;
            .cancel {
              font-size: 16px;
              color: $text-normal;
              margin-right: 20px;
              cursor: pointer;
              &:hover {
                color: $text-333;
              }
            }
            .confirm {
              font-size: 16px;
            }
          }
        }
      }
    }
  }
</style>
